/*
 * Copyright (C) 2012 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.inventory;

import rpg.entities.Entity;
import rpg.gfx.Bitmap;

/**
 * rothens.tumblr.com
 *
 * @author Rothens
 */
public abstract class Item implements Cloneable {

    String name; //name of the item
    int itemID; //ID of the item
    int quantity; //quantity of the item
    int maxSlot; //maximum qty in a slot
    
    ItemType type;
    
    //TODO: not functional yet
    int maxQty; //maximum qty in an inventory

    public Bitmap icon; //icon of the item

    public Item(String name, int itemID) {
        this.name = name;
        this.itemID = itemID;
        quantity = 1;
        maxSlot = 1;
        maxQty = -1;
    }

    public Item(String name, int itemID, int quantity) {
        this(name, itemID);
        this.quantity = quantity;
    }

    public Item(String name, int itemID, int quantity, int maxSlot) {
        this(name, itemID, quantity);
        this.maxSlot = maxSlot;
    }

    public Item(String name, int itemID, int quantity, int maxSlot, int maxQty) {
        this(name, itemID, quantity, maxSlot);
        this.maxQty = maxQty;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    

    /**
     * Consume items. If the slot became empty, it'll return false
     * @return true, if there are remaining items.
     */
    public boolean consume(Entity e) {
        quantity--;
        onConsume(e);
        return quantity > 0;
    }
    
    
    /**
     * Event called after consuming an item.
     * @param e The consumer
     */
    public void onConsume(Entity e){
        
    }
    
    
    /**
     * Aquire items. If there is more item, than the slot maximum, it'll return
     * the number of the remaining items.
     * @param amt items aquired
     * @return remaining items
     */
    public int aquire(int amt, Entity e) {
        int ret = 0;
        int sum = quantity + amt;
        if (sum > maxSlot && maxSlot > 0) {
            ret = sum - maxSlot;
            quantity = maxSlot;
        } else {
            quantity += amt;
        }
        if(ret != amt){
            onAquire(e);
        }
        return ret;
    }
    
    /**
     * Event called when aquiring an item.
     * @param e
     */
    public void onAquire(Entity e) {
        
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getItemID() {
        return itemID;
    }

    public void setItemID(int itemID) {
        this.itemID = itemID;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public ItemType getType() {
        return type;
    }

    public void setType(ItemType type) {
        this.type = type;
    }
}
